namespace ACEMPortal.UserControls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;


	using System.Configuration;
	using ScenarioFramework.WorkflowEngine.DataModel.WorkflowProcesses;
	using Scenario.Security;
    using ScenarioFramework.Modules.DM.DataModel;

	/// <summary>
	///		Summary description for DocumentReports.
	/// </summary>
	public partial class DocumentReports : System.Web.UI.UserControl
	{
		public string sIdDocument = string.Empty;
		public bool AddButtonVisible = true;
		public string ProcessReports = string.Empty;
		XMLDocumentInstance document = null;
		private string sDocumentVersion=string.Empty;

		protected System.Web.UI.WebControls.TextBox txtReport;
		protected System.Web.UI.WebControls.Button btnAddReport;
		
		protected System.Web.UI.WebControls.Label lblReportError;
		protected System.Web.UI.WebControls.Label lblReport;
		protected System.Web.UI.WebControls.CheckBox CheckBox1;
		private string sViewMode=string.Empty;
		private XMLReports processXMLReports = null;
		private bool _SkipMatrixLoad=false;

		public XMLReports GetSelectedReports
		{
			get
			{ 
				XMLReports reports = new XMLReports();

				for (int i=0; i<this.dbDocumentStates.Items.Count; i++)
				{
					CheckBox chk = (CheckBox)this.dbDocumentStates.Items[i].FindControl ("CheckBox1");

					if ( chk.Checked )
					{
						XMLReport NewReport = new XMLReport ();
						NewReport.Date = this.dbDocumentStates.Items[i].Cells[2].Text;
						NewReport.Text = this.dbDocumentStates.Items[i].Cells[3].Text;

						reports.Add(NewReport);

					}
				}

				return reports;

			}
		}

		public bool SkipMatrixLoad
		{
			get{ return _SkipMatrixLoad; }
			set{ _SkipMatrixLoad = value; }
		}

		private void LoadProcessReports()
		{
			if (this.ProcessReports!=null && this.ProcessReports!="")
			{
				try
				{
					processXMLReports = new XMLReports();
					processXMLReports.Parse(ProcessReports);
						
				}
				catch
				{this.processXMLReports = null;}
			}
			else
			{
				this.processXMLReports = null;
			}
		}

		protected void Page_Load(object sender, System.EventArgs e)
		{
			document = (XMLDocumentInstance) Session["DocumentInstance"];
			sDocumentVersion=(string)Session["DocumentVersion"];
            if (document == null)
                return;

			if ( Page.IsPostBack )
			{
				sIdDocument = (string)ViewState["DocumentId"];
				if ( !this.SkipMatrixLoad )
				{
					this.LoadProcessReports();
				}
			}
			else
			{
				if (Request.QueryString["DocumentId"]!= null && Request.QueryString["DocumentId"]!="")
					sIdDocument = Request.QueryString["DocumentId"];
				
				if (Request.QueryString["sm"]!= null && Request.QueryString["sm"]!="")
					sViewMode = Request.QueryString["sm"];

				ViewState["ViewMode"] = sViewMode;
				ViewState["DocumentId"] = sIdDocument;

				document = (XMLDocumentInstance) Session["DocumentInstance"];

				this.LoadProcessReports();

				this.RetrieveReports();
			}
		}

		private void RetrieveReports()
		{
			try
			{
				DataTable dt = new DataTable();
				dt.Columns.Add(new DataColumn("Data", typeof(string)));
				dt.Columns.Add(new DataColumn("Descrizione", typeof(string)));
			
				DataRow dr = null;
				XMLReport report = null;
				XMLReports xmlReports = document.XMLDocumentVersions.Get(sDocumentVersion).XMLReports;
				for (int i=0; i<xmlReports.Count;++i)
				{
					report = (XMLReport)xmlReports[i];			
					dr = dt.NewRow();
 
					dr[0] = report.Date;
					dr[1] = report.Text;
					dt.Rows.Add(dr);
				}
 
				this.dbDocumentStates.DataSource = new DataView(dt);
				this.dbDocumentStates.DataBind();
			}
			catch
			{
				// redirect su pagina di errore Parametri di Activity non presenti
				Response.Redirect( SFPortal.Redirect.GoError("NON E' POSSIBILE LEGGERE I DATI XML"));
			}
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion
	}
}
